package lava.rt.persistence.sql.h2;


import java.sql.SQLException;

import javax.sql.DataSource;

import org.h2.jdbcx.JdbcDataSource;

import lava.rt.persistence.sql.src.DataContextSrcGener;
import lava.rt.persistence.sql.src.H2DataContextSrcGener;
import lava.rt.wrapper.SqlConnectionWrapper;

public class DatacontextSrcGenerMain {

	
	
	public static int initSchema(DataSource dataSource) throws SQLException {
		int ret=0;
		String script=""
				//+"DROP TABLE IF EXISTS `linq_all_column`;\r\n"
				+ "CREATE TABLE `linq_all_column`  (\r\n"
				+ "  `pk` int(255) NOT NULL,\r\n"
				+ "  `col_bigint` bigint(255) NULL DEFAULT NULL,\r\n"
				+ " `col_binary` binary(255) NULL DEFAULT NULL,\r\n"
				+ "  `col_bit` bit(64) NULL DEFAULT NULL,\r\n"
				+ "  `col_blob` blob NULL,\r\n"
				+ "  `col_char` char(1)  DEFAULT NULL,\r\n"
				+ "  `col_date` date NULL DEFAULT NULL,\r\n"
				//+ "`col_enum` enum('Y','N')  NULL DEFAULT NULL,\r\n"
				+ "`col_float` float NULL DEFAULT NULL,\r\n"
				+ "  `col_geometry` geometry NULL,\r\n"
				+ "\r\n"
				+ "`col_int` int(255) NULL DEFAULT NULL,\r\n"
				+ "  `col_integer` int(255) NULL DEFAULT NULL,\r\n"
				+ "  `col_json`  NULL,\r\n"
				+ "  \r\n"
				+ "\r\n"
				+ "`col_real` real NULL DEFAULT NULL,\r\n"
				+ "  `col_smallint` smallint(255) NULL DEFAULT NULL,\r\n"
				+ "\r\n"
				+ "`col_time` time(0) NULL DEFAULT NULL,\r\n"
				+ "\r\n"
				+ "`col_varchar` varchar(255) NULL DEFAULT NULL,\r\n"
				+ " \r\n"
				+ "  PRIMARY KEY (`pk`)\r\n"
				+ ") ";
		
		SqlConnectionWrapper conn=new SqlConnectionWrapper(dataSource.getConnection());
		ret=conn.executeUpdate( script);
		return ret;
	}
	
	public static void main(String[] args) throws Exception {
	    
		 DataSource dataSource=getDataSource();
		 int ret=initSchema(dataSource);
		 println(ret);
		
	     generate(dataSource);
	}

	
	private static void println(Object msg) {
		
		System.out.println(msg);
	}

	private static void generate(DataSource dataSource) throws Exception {
		DataContextSrcGener srcGener=new H2DataContextSrcGener(dataSource.getConnection());
		srcGener.saveAllTo("E:\\git\\lava-rt\\src\\test\\java", LinqDataContext.class, LinqDataContextBase.class, "linq");
	}


	public  static DataSource getDataSource() {
		JdbcDataSource ret=new JdbcDataSource();
		ret.setUrl("jdbc:h2:mem:linq;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
		ret.setUser("sa");
		ret.setPassword("1212");
		return  ret;
	}
	
}
